program ppv;
var	a:array[1..25,1..2] of integer;
	b:array[1..25] of real;
	c:array[1..25] of integer;
	has_money:integer;
	i,j,k,m:integer;
	RJ:real;
	s:integer;
	waste,profit,tmp:integer;
	F:boolean;

procedure sorta(L,R:integer);
var
	i,j:integer;
	w,x:integer;
begin
	i:=L;
	j:=R;
	x:=a[((L+R) div 2),1];
REPEAT
	while a[i,1]<x do {trigger}
		i:=i+1;
	while x<a[j,1] do {trigger}
		j:=j-1;
	if i<=j then begin
		w:=a[i,1];
		a[i,1]:=a[j,1];
		a[j,1]:=w;
		w:=a[j,2];
		a[j,2]:=a[i,2];
		a[i,2]:=w;
		i:=i+1;
		j:=j-1;
	end;
UNTIL i>j;
if L<j then
	sorta(L,j);
if i<R then
	sorta(i,R);
end;

procedure sortb(L,R:integer);
var
	i,j:integer;
	w,x:real;
begin
	i:=L;
	j:=R;
	x:=b[((L+R) div 2)];
repeat
	while b[i]>x do {trigger}
		i:=i+1;
	while x>b[j] do {trigger}
		j:=j-1;
	if i<=j then begin
		w:=b[i];
		b[i]:=b[j];
		b[j]:=w;
		m:=c[j];
		c[j]:=c[i];
		c[i]:=m;
		i:=i+1;
		j:=j-1;
	end;
until i>j;
if L<j then
	sortb(L,j);
if i<R then
	sortb(i,R);
end;


begin
randomize;
for i:=1 to 25 do begin
	a[i,1]:=random(30)+50;
	a[i,2]:=random(9)+10;
end;
sorta(1,25);
for i:=1 to 25 do begin
write(a[i,1]:5);
writeln(a[i,2]:3);
end;
write('we haz $$$: ');readln(has_money);

		{find max in [i,2], check if able to sum up. if not finish summing}
		{восхождение на холм не работает}
		{вернее, выполнено неправильно}
i:=1;j:=1;waste:=0;profit:=0;f:=true;m:=1;tmp:=0;

while (j<=25) and (j>=1) do begin
	if tmp<a[j,2] then begin {trigger}
		tmp:=a[j,2];
		s:=j;
	end;
	j:=j+1;end;
k:=tmp;

while (waste<=has_money) and f and (m<=25) do begin {началось}
	if (waste+tmp<=has_money) then begin
		waste:=waste+a[s,1];
		profit:=profit+a[s,2];end
	else
		f:=false;
	k:=k-1;
	for i:=25 to 1 do
		if a[i,2]=k then
			s:=i;
	m:=m+1;
end;
writeln(waste:5,profit:7);

		{easiest one}
waste:=0;profit:=0;j:=1;i:=1;f:=true;
while f do begin
	if (waste+a[i,1]<=has_money) then begin
		waste:=waste+a[i,1];
		profit:=profit+a[i,2];end
	else
		f:=false;
	i:=i+1;
end;
writeln(waste:5,profit:7);

		{best one actually}
J:=1;waste:=0;profit:=0;
for i:=1 to 25 do begin
	b[i]:=(a[i,2]/a[i,1]);
	c[i]:=j;
	j:=j+1;
end;
sortb(1,25);
{debug--for i:=1 to 25 do writeln(b[i]:9:4,c[i]:8);}

RJ:=1;f:=true;i:=1;
while f do begin
	if (waste+a[c[i],1]<=has_money) then begin
		waste:=waste+a[(c[i]),1];{debug--writeln('waste+',i:4);}
		profit:=profit+a[(c[i]),2];{debug--writeln('profit',c[i]:4);}
		i:=i+1;end
	else
		f:=false;
end;
writeln(waste:5,profit:7);
end.
